{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf350
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;\red128\green128\blue128;
\red0\green128\blue0;\red163\green21\blue21;}
\paperw11900\paperh16840\vieww25740\viewh23580\viewkind0
\deftab720
\pard\pardeftab720\ql\qnatural

\f0\fs19 \cf2 using\cf0  System;\
\cf2 using\cf0  System.Text;\
\cf2 using\cf0  System.Net;\
\cf2 using\cf0  System.IO;\
\cf2 using\cf0  System.Xml;\
\cf2 using\cf0  System.Collections;\
\
\cf2 namespace\cf0  TextMarketer \{\
	\cf2 class\cf0  \cf3 Example\cf0  \{\
\
		\cf4 ///\cf5  \cf4 <summary>\cf0 \
		\cf4 ///\cf5  Example code to demonstrate the use of the SendSms(...) static method.\cf0 \
		\cf4 ///\cf5  \cf4 </summary>\cf0 \
		\cf4 ///\cf5  \cf4 <param name="args"></param>\cf0 \
		\cf2 static\cf0  \cf2 void\cf0  Main(\cf2 string\cf0 [] args) \{\
\
			\cf5 //Declare the OUT parameters that are used to return the results of the SendSms request\cf0 \
			\cf3 DateTime\cf0  processedDate;\
			\cf2 string\cf0  messageId;\
			\cf2 int\cf0  creditsUsed;\
			\cf3 TextMarketerError\cf0 [] errors;\
\
			\cf5 //Replace these parameters with your own\cf0 \
			\cf2 bool\cf0  success = SendSms(\cf6 "This is my message"\cf0 , \cf6 "018118055"\cf0 , \cf6 "me"\cf0 , \cf6 "myUserName"\cf0 , \cf6 "myPassword"\cf0 , \cf2 out\cf0  processedDate, \cf2 out\cf0  messageId, \cf2 out\cf0  creditsUsed, \cf2 out\cf0  errors);\
			\cf2 if\cf0  (success) \{\
				\cf5 //Show the results of a successful request\cf0 \
				\cf3 Console\cf0 .WriteLine(\cf6 "SMS sent OK on "\cf0  + processedDate.ToString() + \cf6 ". MessageID="\cf0  + messageId + \cf6 ", Credits Used="\cf0  + creditsUsed);\
			\}\
			\cf2 else\cf0  \{\
				\cf5 //Show the results on an unsuccessful request\cf0 \
				\cf3 Console\cf0 .WriteLine(\cf6 "SMS sending failed with the following errors:"\cf0 );\
				\cf2 foreach\cf0  (\cf3 TextMarketerError\cf0  error \cf2 in\cf0  errors) \{\
					\cf3 Console\cf0 .WriteLine(\cf6 "Code: "\cf0  + error.errorCode + \cf6 "  Text: "\cf0  + error.errorText);\
				\}\
			\}\
		\}\
\
		\cf4 ///\cf5  \cf4 <summary>\cf0 \
		\cf4 ///\cf5  This method sends an SMS request to the Text Marketer SMS web service, and returns details of the result of the request.\cf0 \
		\cf4 ///\cf5  \cf4 </summary>\cf0 \
		\cf4 ///\cf5  \cf4 <param name="message">\cf5 The body of the text message to send\cf4 </param>\cf0 \
		\cf4 ///\cf5  \cf4 <param name="mobileNumber">\cf5 The phone number to send the text message to\cf4 </param>\cf0 \
		\cf4 ///\cf5  \cf4 <param name="originator">\cf5 Text or a phone number, to indicate who the sender is to the recipient\cf4 </param>\cf0 \
		\cf4 ///\cf5  \cf4 <param name="username">\cf5 The username of your Text Marketer account\cf4 </param>\cf0 \
		\cf4 ///\cf5  \cf4 <param name="password">\cf5 The password of your Text Marketer account\cf4 </param>\cf0 \
		\cf4 ///\cf5  \cf4 <param name="processedDate">\cf5 Returns the date and time at which the server processed your request\cf4 </param>\cf0 \
		\cf4 ///\cf5  \cf4 <param name="messageId">\cf5 Returns a unique Id by which the server identifies the message\cf4 </param>\cf0 \
		\cf4 ///\cf5  \cf4 <param name="creditsUsed">\cf5 Returns the number of Text Marketer credits consumed by sending the text message\cf4 </param>\cf0 \
		\cf4 ///\cf5  \cf4 <param name="errors"></param>\cf5 Returns an array of TextMarketerError objects identifying an errors in your request.\cf0 \
		\cf4 ///\cf5  \cf4 <returns>\cf5 True if the request was successful, false if the server could not process your request (see 'errors' out-parameter for details)\cf4 </returns>\cf0 \
		\cf2 public\cf0  \cf2 static\cf0  \cf2 bool\cf0  SendSms(\cf2 string\cf0  message, \cf2 string\cf0  mobileNumber, \cf2 string\cf0  originator, \cf2 string\cf0  username, \cf2 string\cf0  password, \cf2 out\cf0  \cf3 DateTime\cf0  processedDate, \cf2 out\cf0  \cf2 string\cf0  messageId, \cf2 out\cf0  \cf2 int\cf0  creditsUsed, \cf2 out\cf0  \cf3 TextMarketerError\cf0 [] errors) \{\
\
			\cf5 //Create a string representing the data elements to post\cf0 \
			\cf3 StringBuilder\cf0  postData = \cf2 new\cf0  \cf3 StringBuilder\cf0 ();\
			postData.Append(\cf6 "message="\cf0 ).Append(message);\
			postData.Append(\cf6 "&mobile_number="\cf0 ).Append(mobileNumber);\
			postData.Append(\cf6 "&originator="\cf0 ).Append(originator);\
			postData.Append(\cf6 "&username="\cf0 ).Append(username);\
			postData.Append(\cf6 "&password="\cf0 ).Append(password);\
\
			\cf5 //Encode the data string into ASCII bytes\cf0 \
			\cf3 ASCIIEncoding\cf0  encoding = \cf2 new\cf0  \cf3 ASCIIEncoding\cf0 ();\
			\cf2 byte\cf0 [] data = encoding.GetBytes(postData.ToString());\
\
			\cf5 //Create the web request to the Text Marketer web service and attach the data\cf0 \
			\cf3 HttpWebRequest\cf0  webRequest = (\cf3 HttpWebRequest\cf0 )\cf3 WebRequest\cf0 .Create(\cf6 "http://www.textmarketer.biz/services/rest/sms"\cf0 );\
			webRequest.Method = \cf6 "POST"\cf0 ;\
			webRequest.ContentType = \cf6 "application/x-www-form-urlencoded"\cf0 ;\
			webRequest.ContentLength = data.Length;\
			\cf3 Stream\cf0  newStream = webRequest.GetRequestStream();\
			newStream.Write(data, 0, data.Length);\
			newStream.Close();\
\
			\cf2 string\cf0  resultString;\
			\cf2 try\cf0  \{\
				\cf5 //Action the post to the server and get the response data stream\cf0 \
				\cf3 HttpWebResponse\cf0  webResponse = (\cf3 HttpWebResponse\cf0 )webRequest.GetResponse();\
				\cf2 using\cf0  (\cf3 StreamReader\cf0  streamReader = \cf2 new\cf0  \cf3 StreamReader\cf0 (webResponse.GetResponseStream())) \{\
					resultString = streamReader.ReadToEnd();\
					streamReader.Close();\
				\}\
				\cf5 //Convert the response data into an XML document and parse the expected elements to get the individual response fields\cf0 \
				\cf3 XmlDocument\cf0  xmlDocument = \cf2 new\cf0  \cf3 XmlDocument\cf0 ();\
				xmlDocument.LoadXml(resultString);\
				processedDate = \cf3 DateTime\cf0 .Parse(xmlDocument.GetElementsByTagName(\cf6 "response"\cf0 )[0].Attributes.GetNamedItem(\cf6 "processed_date"\cf0 ).Value);\
				messageId = xmlDocument.GetElementsByTagName(\cf6 "message_id"\cf0 ).Item(0).InnerText;\
				creditsUsed = \cf3 Int32\cf0 .Parse(xmlDocument.GetElementsByTagName(\cf6 "credits_used"\cf0 ).Item(0).InnerText);\
				errors = \cf2 null\cf0 ;\
				\cf2 return\cf0  \cf2 true\cf0 ;\
			\}\
			\cf2 catch\cf0  (\cf3 WebException\cf0  e) \{\
				\cf5 //If we get here, the service returned a server error, so read the server response from the exception\cf0 \
				\cf2 using\cf0  (\cf3 StreamReader\cf0  streamReader = \cf2 new\cf0  \cf3 StreamReader\cf0 (e.Response.GetResponseStream())) \{\
					resultString = streamReader.ReadToEnd();\
					streamReader.Close();\
				\}\
				\cf5 //Convert the response data into an XML document and parse the expected error elements to get the individual response fields\cf0 \
				\cf3 XmlDocument\cf0  xmlDocument = \cf2 new\cf0  \cf3 XmlDocument\cf0 ();\
				xmlDocument.LoadXml(resultString);\
				processedDate = \cf3 DateTime\cf0 .Parse(xmlDocument.GetElementsByTagName(\cf6 "response"\cf0 )[0].Attributes.GetNamedItem(\cf6 "processed_date"\cf0 ).Value);\
				\cf3 XmlNodeList\cf0  errorsNodeList = xmlDocument.GetElementsByTagName(\cf6 "errors"\cf0 );\
				\cf3 XmlNodeList\cf0  errorNodeList = errorsNodeList.Item(0).ChildNodes;\
				errors = \cf2 new\cf0  \cf3 TextMarketerError\cf0 [errorNodeList.Count];\
				\cf3 IEnumerator\cf0  errorNodeListEnum = errorNodeList.GetEnumerator();\
				\cf2 int\cf0  i = 0;\
				\cf2 while\cf0  (errorNodeListEnum.MoveNext()) \{\
					\cf3 XmlNode\cf0  errorNode = (\cf3 XmlNode\cf0 )errorNodeListEnum.Current;\
					\cf2 string\cf0  errorCode = errorNode.Attributes.GetNamedItem(\cf6 "code"\cf0 ).Value;\
					\cf2 string\cf0  errorText = errorNode.InnerText;\
					\cf3 TextMarketerError\cf0  error = \cf2 new\cf0  \cf3 TextMarketerError\cf0 (\cf3 Int32\cf0 .Parse(errorCode), errorText);\
					errors[i++] = error;\
				\}\
				messageId = \cf2 null\cf0 ;\
				creditsUsed = 0;\
				\cf2 return\cf0  \cf2 false\cf0 ;\
			\}\
		\}\
\
		\cf4 ///\cf5  \cf4 <summary>\cf0 \
		\cf4 ///\cf5  Represents an individual error response element from the Text Marketer SMS service\cf0 \
		\cf4 ///\cf5  \cf4 </summary>\cf0 \
		\cf2 public\cf0  \cf2 class\cf0  \cf3 TextMarketerError\cf0  \{\
\
			\cf2 public\cf0  \cf2 int\cf0  errorCode;\
			\cf2 public\cf0  \cf3 String\cf0  errorText;\
\
			\cf2 public\cf0  TextMarketerError(\cf2 int\cf0  errorCode, \cf2 string\cf0  errorText) \{\
				\cf2 this\cf0 .errorCode = errorCode;\
				\cf2 this\cf0 .errorText = errorText;\
			\}\
		\}\
	\}\
\}\
\

\fs22 \
}